package report

import (
	"../common"
	"../web"
	"encoding/json"
	"strconv"
	"strings"
)

type QueryWoPassedObj struct {
	common.BaseHandler
}

type WoQueryPassedPageObj struct {
	common.BaseHandler
}

func (self *QueryWoPassedObj) Prepare() {
	stations := *common.FetchAllRowsPtr(
		`SELECT STATION, ''
      	   FROM SFC_STATION(NOLOCK) 
    	  ORDER BY STATION`,
		self.Usr.Conn, new(common.Station))
	self.TmplData["stations"] = stations
}

func (self *QueryWoPassedObj) ParameterCheck() bool {
	s_wono := strings.TrimSpace(self.Ctx.Params["wono"])
	s_stat := strings.TrimSpace(self.Ctx.Params["station"])
	if s_wono == "" {
		return false
	}

	self.Sqlparas = append(self.Sqlparas, s_wono)
	self.Sqlparas = append(self.Sqlparas, s_stat)

	return true
}

func (self *QueryWoPassedObj) SqlQueryExecute() {
	s_wono := self.Sqlparas[0].(string)
	s_stat := self.Sqlparas[1].(string)
	var wopass []interface{}
	var iRtnData interface{}
	distinctNum := 0
	curr_id := 1
	recnum := 0
	if s_stat == "" {
		wopass = *common.FetchAllRowsPtr(
			`SELECT T.AUTOID,T.ITEM,T.DESCRIP,T.SSN_NO,T.CURR_STATION,T.RESULT,
						 ISNULL(Z.NAME,T.MODIFYBY),T.MDATE 
			   FROM (
					SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.SSN_NO, A.IDX) AS AUTOID, 
							C.ITEM, ISNULL(D.DESCRIP, '') AS DESCRIP, A.SSN_NO, 
							A.CURR_STATION, A.RESULT, A.MODIFYBY, 
							CONVERT(CHAR(23), A.MODIFYDATE, 121) AS MDATE
      				  FROM SFC_SSNFLOW A(NOLOCK), SFC_SSN B(NOLOCK), SFC_WO C(NOLOCK), 
      				  		SFC_ITEMS D(NOLOCK)
     				 WHERE B.WONO=?
       				   AND A.SSN_NO=B.SSN_NO
       				   AND B.WONO=C.WONO
       				   AND C.ITEM=D.ITEM) T
			   LEFT JOIN ShopFloorControl.dbo.SFC_EMPLOYEE Z(NOLOCK)
				 ON T.MODIFYBY=Z.ID`,
			self.Usr.Conn, new(common.WoPassed), s_wono)

		iRtnData = *common.FetchOneRowPtr(
			`SELECT COUNT(SSN_NO) 
			   FROM SFC_SSNFLOW(NOLOCK) 
			  WHERE SSN_NO IN (SELECT SSN_NO FROM SFC_SSN(NOLOCK) 
			  		WHERE WONO=?)`,
			self.Usr.Conn, new(common.RecCount), s_wono)
		recnum = iRtnData.(common.RecCount).Number
	} else {
		wopass = *common.FetchAllRowsPtr(
			`SELECT T.AUTOID,T.ITEM,T.DESCRIP,T.SSN_NO,T.CURR_STATION,T.RESULT,
					 ISNULL(Z.NAME,T.MODIFYBY),T.MDATE 
			   FROM (
					SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY A.SSN_NO, A.IDX) AS AUTOID, 
							C.ITEM, ISNULL(D.DESCRIP, '') AS DESCRIP, A.SSN_NO, 
							A.CURR_STATION, A.RESULT, A.MODIFYBY, 
							CONVERT(CHAR(23), A.MODIFYDATE, 121) AS MDATE
      				  FROM SFC_SSNFLOW A(NOLOCK), SFC_SSN B(NOLOCK), SFC_WO C(NOLOCK), 
      				  		SFC_ITEMS D(NOLOCK)
     				 WHERE B.WONO=?
       				   AND A.SSN_NO=B.SSN_NO
       				   AND A.CURR_STATION=?
       				   AND B.WONO=C.WONO
       				   AND C.ITEM=D.ITEM) T
			   LEFT JOIN ShopFloorControl.dbo.SFC_EMPLOYEE Z(NOLOCK)
				 ON T.MODIFYBY=Z.ID`,
			self.Usr.Conn, new(common.WoPassed), s_wono, s_stat)

		iRtnData = *common.FetchOneRowPtr(
			`SELECT COUNT(DISTINCT SSN_NO) 
			   FROM SFC_SSNFLOW(NOLOCK) 
			  WHERE SSN_NO IN (SELECT SSN_NO FROM SFC_SSN(NOLOCK) WHERE WONO=?) 
				AND CURR_STATION=?`,
			self.Usr.Conn, new(common.RecCount), s_wono, s_stat)
		distinctNum = iRtnData.(common.RecCount).Number

		iRtnData = *common.FetchOneRowPtr(
			`SELECT COUNT(SSN_NO) 
			   FROM SFC_SSNFLOW(NOLOCK) 
			  WHERE SSN_NO IN (SELECT SSN_NO FROM SFC_SSN(NOLOCK) WHERE WONO=?) 
				AND CURR_STATION=?`,
			self.Usr.Conn, new(common.RecCount), s_wono, s_stat)
		recnum = iRtnData.(common.RecCount).Number
	}

	total_id := recnum / 10
	if recnum%10 > 0 {
		total_id = total_id + 1
	}

	self.TmplData["wono"] = s_wono
	self.TmplData["stat"] = s_stat
	self.TmplData["wopass"] = wopass
	self.TmplData["distinct_num"] = distinctNum
	self.TmplData["curr_page"] = curr_id
	self.TmplData["total_page"] = total_id
}

func QueryWoPassedHandler(ctx *web.Context) {
	self := new(QueryWoPassedObj)
	self.Ctx = ctx
	self.Html = "wopassed.html"
	self.Rightlist = []string{"wopassed"}
	self.DoQuery(self)
}

func (self *WoQueryPassedPageObj) ParameterCheck() bool {
	s_wono := self.Ctx.Params["wono"]
	s_stat := self.Ctx.Params["station"]
	s_page := self.Ctx.Params["pageid"]
	if s_wono == "" {
		return false
	}
	pageid, err := strconv.Atoi(s_page)
	if err != nil {
		return false
	}

	idx := 10 * (pageid - 1)

	self.Sqlparas = append(self.Sqlparas, s_wono)
	self.Sqlparas = append(self.Sqlparas, s_stat)
	self.Sqlparas = append(self.Sqlparas, idx)

	return true
}

func (self *WoQueryPassedPageObj) SqlQueryExecute() {
	s_wono := self.Sqlparas[0].(string)
	s_stat := self.Sqlparas[1].(string)
	idx := self.Sqlparas[2].(int)
	var wonos []interface{}
	if s_stat == "" {
		wonos = *common.FetchAllRowsPtr(
			`SELECT TOP 10 A.AUTOID,A.ITEM,A.DESCRIP,A.SSN_NO,A.CURR_STATION,A.RESULT,A.OPER,A.MDATE
			   FROM 
			   		(
			   			SELECT T.AUTOID,T.ITEM,T.DESCRIP,T.SSN_NO,T.CURR_STATION,T.RESULT,
						 		ISNULL(Z.NAME,T.MODIFYBY) AS OPER,T.MDATE 
				   		  FROM (
								 SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO, A.IDX) AS AUTOID, 
										 C.ITEM, ISNULL(D.DESCRIP, '') AS DESCRIP, A.SSN_NO, 
										 A.CURR_STATION, A.RESULT, A.MODIFYBY, 
										 CONVERT(CHAR(23), A.MODIFYDATE, 121) AS MDATE
          				  		   FROM SFC_SSNFLOW A(NOLOCK), SFC_SSN B(NOLOCK), SFC_WO C(NOLOCK), 
          				  				 SFC_ITEMS D(NOLOCK)
         				 		  WHERE B.WONO=?
           				   			AND A.SSN_NO=B.SSN_NO
           				   			AND B.WONO=C.WONO
           				   			AND C.ITEM=D.ITEM) T
		   				  LEFT JOIN ShopFloorControl.dbo.SFC_EMPLOYEE Z(NOLOCK)
			 				ON T.MODIFYBY=Z.ID
			   		) A
			  WHERE A.AUTOID > ?`,
			self.Usr.Conn, new(common.WoPassed), s_wono, idx)
	} else {
		wonos = *common.FetchAllRowsPtr(
			`SELECT TOP 10 A.AUTOID,A.ITEM,A.DESCRIP,A.SSN_NO,A.CURR_STATION,A.RESULT,A.OPER,A.MDATE
			   FROM 
			   		(
			   			SELECT T.AUTOID,T.ITEM,T.DESCRIP,T.SSN_NO,T.CURR_STATION,T.RESULT,
						 		ISNULL(Z.NAME,T.MODIFYBY) AS OPER,T.MDATE 
				   		  FROM (
								 SELECT ROW_NUMBER() OVER(ORDER BY A.SSN_NO, A.IDX) AS AUTOID, 
										 C.ITEM, ISNULL(D.DESCRIP, '') AS DESCRIP, A.SSN_NO, 
										 A.CURR_STATION, A.RESULT, A.MODIFYBY, 
										 CONVERT(CHAR(23), A.MODIFYDATE, 121) AS MDATE
          				  		   FROM SFC_SSNFLOW A(NOLOCK), SFC_SSN B(NOLOCK), SFC_WO C(NOLOCK), 
          				  				 SFC_ITEMS D(NOLOCK)
         				 		  WHERE B.WONO=?
           				   			AND A.SSN_NO=B.SSN_NO
           				   			AND A.CURR_STATION=?
           				   			AND B.WONO=C.WONO
           				   			AND C.ITEM=D.ITEM) T
		   				  LEFT JOIN ShopFloorControl.dbo.SFC_EMPLOYEE Z(NOLOCK)
			 				ON T.MODIFYBY=Z.ID
			   		) A
			  WHERE A.AUTOID > ?`,
			self.Usr.Conn, new(common.WoPassed), s_wono, s_stat, idx)
	}

	dict_js, _ := json.Marshal(map[string]interface{}{"wonos": wonos, "lang": self.Lang})
	self.Ctx.ResponseWriter.Write(dict_js)
}

func SplitWoPassedByPageHandler(ctx *web.Context) {
	self := new(WoQueryPassedPageObj)
	self.Ctx = ctx
	self.DoJsonQuery(self)
}
